//
//  PzHomeGuideView.swift
//  Pz
//
//  Created by 蓝鳍互娱 on 2024/6/26.
//

import Foundation
import UIKit
import SnapKit
class PzMeHeadView: UIView {
    var openVipBlock: (() -> Void)?

    override init(frame: CGRect) {
        super.init(frame: frame)
        creatSubView()
        updataVipStatus()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var meHeadBgImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "pzMeHeaBg")
        return imageView
    }()
    lazy var meHeadTitleLab: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 38.fitScale(), weight: .regular)
        lab.textColor = UIColor(hex:0x282832)
        return lab
    }()
    lazy var meHeadTitleSubLab: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 28.fitScale(), weight: .regular)
        lab.textColor = UIColor(hex:0x322F2F)
        return lab
    }()
    lazy var meHeadOpenVipBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(meHeadOpenVip), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "pzMeOpenVipIcon"), for: .normal)
        return btn
    }()
    func creatSubView() {
        self.addSubview(meHeadBgImgView)
        self.addSubview(meHeadTitleLab)
        self.addSubview(meHeadTitleSubLab)
        self.addSubview(meHeadOpenVipBtn)
        
        meHeadBgImgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        meHeadTitleLab.snp.makeConstraints { make in
            make.left.equalTo(42.fitScale())
            make.top.equalTo(28.fitScale())
            make.height.equalTo(54.fitScale())
        }
        meHeadTitleSubLab.snp.makeConstraints { make in
            make.left.equalTo(42.fitScale())
            make.top.equalTo(79.fitScale())
            make.height.equalTo(40.fitScale())
        }
        meHeadOpenVipBtn.snp.makeConstraints { make in
            make.right.equalTo(-25.fitScale())
            make.top.equalTo(44.fitScale())
            make.width.equalTo(144.fitScale())
            make.height.equalTo(58.fitScale())
        }
    }
    func updataVipStatus(){
        let vipTime = UserDefaults.standard.value(forKey: "pzDefTime") as? Double ?? 0
        let cureentTime = PzPublicConst.photoNowTime
        if cureentTime >= vipTime{//非会员
            meHeadTitleLab.text = "成为会员"
            meHeadTitleSubLab.text = "开通畅享众多使用特权"
            meHeadOpenVipBtn.isHidden = false
        }else{//会员
            meHeadTitleLab.text = "你已是会员"
//            meHeadTitleSubLab.text = "会员有效期至:" + makeVipTime(vipTime: vipTime)//免费版
            meHeadOpenVipBtn.isHidden = true
        }
    }
    func makeVipTime(vipTime:Double) -> String{
        let date = NSDate(timeIntervalSince1970: vipTime)
        let dateFormat = DateFormatter()
        dateFormat.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let formatDate = dateFormat.string(from: date as Date)
        return formatDate
        
    }
    @objc func meHeadOpenVip(){
        self.openVipBlock?()
    }
}
